package com.weiadmin.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiadmin.common.BaseController;
import com.weiadmin.common.PageParam;
import com.weiadmin.dao.SlideDao;
import com.weiadmin.dao.SlideItemDao;
import com.weiadmin.model.system.SlideModel;
import com.weiadmin.model.system.SlideItemModel;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 功能描述
 *
 * @author: shizhongyu
 * @date: 2023年06月05日 9:31
 */
@RestController
@RequestMapping("/admin/slideItem")
public class SlideItemController extends BaseController {
    @Autowired
    SlideItemDao dao;

    @Autowired
    SlideDao slideDao;
    private String module = "slideItem";

    @GetMapping("/index")
    public ModelAndView index(Model model) {
        ModelAndView modelAndView = display(this.module+"/index");
        return modelAndView;
    }
    @GetMapping("/list")
    public ResponseEntity<?> list(PageParam pageParam)
    {
        Page<SlideItemModel> result =  dao.getList(pageParam);
        return page("请求成功",result);
    }

    @GetMapping("/add")
    public ModelAndView add(Model model) {
        ModelAndView modelAndView = display(this.module+"/add");
        //幻灯片分类列表
        QueryWrapper<SlideModel> queryWrapper = new QueryWrapper<>();
        List<SlideModel> cateList =  slideDao.selectList(queryWrapper);
        model.addAttribute("cateList",cateList);
        return modelAndView;
    }

    @GetMapping("/edit")
    public ModelAndView edit(Model model,String id) {
        ModelAndView modelAndView = display(this.module+"/edit");
        SlideItemModel item =  dao.selectById(id);
        model.addAttribute("item",item);
        //幻灯片分类列表
        QueryWrapper<SlideModel> queryWrapper = new QueryWrapper<>();
        List<SlideModel> cateList =  slideDao.selectList(queryWrapper);
        model.addAttribute("cateList",cateList);
        return modelAndView;
    }

    @PostMapping("/addPost")
    public ResponseEntity<?> addPost(@RequestBody SlideItemModel params)
    {
        dao.insert(params);
        return success("添加成功！");
    }

    @PostMapping("/editPost")
    public ResponseEntity<?> editPost(@RequestBody SlideItemModel params)
    {
        dao.updateById(params);
        return success("保存成功！");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable String id)
    {
        if(StringUtils.isEmpty(id)){
            return error("ID不能为空！");
        }
        int result = dao.deleteById(id);
        if(result>0){
            return success("删除成功！");
        }else{
            return error("删除失败,请稍后再试！");
        }
    }
}
